package com.polaris.live.adapter.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.ext.autoDp
import com.polaris.live.databinding.AdapterHotListWomanBinding
import com.polaris.live.databinding.AdapterListBannerBinding
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.utils.state.UserStateUtils
import com.polaris.live.widget.avatar.FrameImageUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * HomeWomanAdapter
 *
 * @author Created by lucas on 2023/10/17 15:12
 * @since 1.0.0
 */
class HomeWomanAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    init {
        addChildClickViewIds(R.id.cl_invite)
    }

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is DisplayUserBean -> HomeManAdapter.TYPE_NORMAL
            is ListWithBannerBean -> HomeManAdapter.TYPE_BANNER
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            HomeManAdapter.TYPE_BANNER -> BannerViewHolder(
                AdapterListBannerBinding.inflate(layoutInflater, parent, false)
            )

            HomeManAdapter.TYPE_NORMAL -> NormalWomanViewHolder(
                AdapterHotListWomanBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is NormalWomanViewHolder -> if (item is DisplayUserBean) holder.bind(item)
            is BannerViewHolder -> if (item is ListWithBannerBean) holder.bind(item)
        }
    }

    class NormalWomanViewHolder(
        binding: AdapterHotListWomanBinding,
    ) : BaseBindingViewHolder<AdapterHotListWomanBinding, DisplayUserBean>(binding) {

        override fun bind(item: DisplayUserBean) {
            mBinding.apply {
                avatar.setImageUrl(item.base.avatar)
                avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(item.base.avatarFrame))
                tvNickname.text = item.base.nickname
                cvCountry.setCountry(item.base.country)
                lvLanguage.setLanguage(item.base.secondLanguage, 10.autoDp, 10.autoDp, 6)
                vipView.setVipLevel(item.base.vipLevel)
                level.setRichCharmBySexLevel(
                    item.base.sex,
                    item.base.richs,
                    item.base.charms
                )
                val state = UserStateUtils.resolveUserState(item.base)
                when (state) {
                    UserStateUtils.USER_STATE_ONLINE, UserStateUtils.USER_STATE_OFFLINE, UserStateUtils.USER_STATE_BUSY -> {
                        vState.visibility = View.VISIBLE
                        ivStateLive.visibility = View.GONE
                        when (state) {
                            UserStateUtils.USER_STATE_ONLINE -> {
                                vState.setBackgroundResource(R.drawable.ic_user_space)
                            }

                            UserStateUtils.USER_STATE_OFFLINE -> {
                                vState.setBackgroundResource(R.drawable.shape_session_state_offline)
                            }

                            UserStateUtils.USER_STATE_BUSY -> {
                                vState.setBackgroundResource(R.drawable.ic_user_space)
//                                vState.setBackgroundResource(R.drawable.shape_session_state_busy)
                            }

                            else -> {}
                        }
                    }

                    UserStateUtils.USER_STATE_LIVE, UserStateUtils.USER_STATE_MATCH, UserStateUtils.USER_STATE_PARTY, UserStateUtils.USER_STATE_GAME -> {
                        vState.visibility = View.GONE
                        ivStateLive.visibility = View.VISIBLE
                        when (state) {
                            UserStateUtils.USER_STATE_LIVE -> {
                                vState.visibility = View.VISIBLE
                                ivStateLive.visibility = View.GONE
                                vState.setBackgroundResource(R.drawable.ic_user_space)
//                                ivStateLive.setImageResource(null)
                            }

                            UserStateUtils.USER_STATE_MATCH -> {
                                vState.visibility = View.VISIBLE
                                ivStateLive.visibility = View.GONE
                                vState.setBackgroundResource(R.drawable.ic_user_space)
//                                ivStateLive.setImageResource(R.drawable.icon_list_match)
                            }

                            UserStateUtils.USER_STATE_PARTY -> {
                                ivStateLive.setImageResource(R.drawable.icon_party)
                            }

                            UserStateUtils.USER_STATE_GAME -> {
                                ivStateLive.setImageResource(R.drawable.icon_list_gameing)
                            }

                            else -> {}
                        }
                    }

                    else -> {
                        vState.visibility = View.GONE
                        ivStateLive.visibility = View.GONE
                    }
                }
            }
        }
    }
}